package org.framework.lazy.cloud.network.heartbeat.server.standalone.infrastructure.persistence;

import jakarta.annotation.Resource;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.proxy.flow.LazyProxyFlow;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.lazy.proxy.flow.LazyProxyFlowRepository;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.infrastructure.converter.LazyProxyFlowConverter;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.infrastructure.entity.LazyProxyFlowDO;
import org.springframework.stereotype.Repository;
import org.wu.framework.core.utils.ObjectUtils;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * describe 代理流量
 *
 * @author Jia wei Wu
 * @date 2025/06/06 11:39 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class LazyProxyFlowRepositoryImpl implements LazyProxyFlowRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * describe 新增代理流量
     *
     * @param lazyProxyFlow 新增代理流量
     * @return {@link Result<LazyProxyFlow>} 代理流量新增后领域对象
     * @author Jia wei Wu
     * @date 2025/06/06 11:39 晚上
     **/

    @Override
    public Result<LazyProxyFlow> story(LazyProxyFlow lazyProxyFlow) {
        LazyProxyFlowDO lazyProxyFlowDO = LazyProxyFlowConverter.INSTANCE.fromLazyProxyFlow(lazyProxyFlow);
        lazyLambdaStream.upsert(lazyProxyFlowDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增代理流量
     *
     * @param lazyProxyFlowList 批量新增代理流量
     * @return {@link Result<List<LazyProxyFlow>>} 代理流量新增后领域对象集合
     * @author Jia wei Wu
     * @date 2025/06/06 11:39 晚上
     **/

    @Override
    public Result<List<LazyProxyFlow>> batchStory(List<LazyProxyFlow> lazyProxyFlowList) {
        List<LazyProxyFlowDO> lazyProxyFlowDOList = lazyProxyFlowList.stream().map(LazyProxyFlowConverter.INSTANCE::fromLazyProxyFlow).collect(Collectors.toList());
        lazyLambdaStream.upsert(lazyProxyFlowDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个代理流量
     *
     * @param lazyProxyFlow 查询单个代理流量
     * @return {@link Result<LazyProxyFlow>} 代理流量领域对象
     * @author Jia wei Wu
     * @date 2025/06/06 11:39 晚上
     **/

    @Override
    public Result<LazyProxyFlow> findOne(LazyProxyFlow lazyProxyFlow) {
        LazyProxyFlowDO lazyProxyFlowDO = LazyProxyFlowConverter.INSTANCE.fromLazyProxyFlow(lazyProxyFlow);
        LazyProxyFlow lazyProxyFlowOne = lazyLambdaStream.selectOne(LazyWrappers.lambdaWrapperBean(lazyProxyFlowDO), LazyProxyFlow.class);
        return ResultFactory.successOf(lazyProxyFlowOne);
    }

    /**
     * describe 查询多个代理流量
     *
     * @param lazyProxyFlow 查询多个代理流量
     * @return {@link Result<List<LazyProxyFlow>>} 代理流量领域对象
     * @author Jia wei Wu
     * @date 2025/06/06 11:39 晚上
     **/

    @Override
    public Result<List<LazyProxyFlow>> findList(LazyProxyFlow lazyProxyFlow) {
        LazyProxyFlowDO lazyProxyFlowDO = LazyProxyFlowConverter.INSTANCE.fromLazyProxyFlow(lazyProxyFlow);
        List<LazyProxyFlow> lazyProxyFlowList = lazyLambdaStream.selectList(LazyWrappers.lambdaWrapperBean(lazyProxyFlowDO), LazyProxyFlow.class);
        return ResultFactory.successOf(lazyProxyFlowList);
    }

    /**
     * describe 分页查询多个代理流量
     *
     * @param size          当前页数
     * @param current       当前页
     * @param lazyProxyFlow 分页查询多个代理流量
     * @return {@link Result<LazyPage<LazyProxyFlow>>} 分页代理流量领域对象
     * @author Jia wei Wu
     * @date 2025/06/06 11:39 晚上
     **/

    @Override
    public Result<LazyPage<LazyProxyFlow>> findPage(int size, int current, LazyProxyFlow lazyProxyFlow) {
        LazyProxyFlowDO lazyProxyFlowDO = LazyProxyFlowConverter.INSTANCE.fromLazyProxyFlow(lazyProxyFlow);
        LazyPage<LazyProxyFlow> lazyPage = new LazyPage<>(current, size);
        LazyPage<LazyProxyFlow> lazyProxyFlowLazyPage = lazyLambdaStream.selectPage(LazyWrappers.lambdaWrapperBean(lazyProxyFlowDO), lazyPage, LazyProxyFlow.class);
        return ResultFactory.successOf(lazyProxyFlowLazyPage);
    }

    /**
     * describe 删除代理流量
     *
     * @param lazyProxyFlow 删除代理流量
     * @return {@link Result<LazyProxyFlow>} 代理流量
     * @author Jia wei Wu
     * @date 2025/06/06 11:39 晚上
     **/

    @Override
    public Result<LazyProxyFlow> remove(LazyProxyFlow lazyProxyFlow) {
        LazyProxyFlowDO lazyProxyFlowDO = LazyProxyFlowConverter.INSTANCE.fromLazyProxyFlow(lazyProxyFlow);
        lazyLambdaStream.delete(LazyWrappers.lambdaWrapperBean(lazyProxyFlowDO));
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在代理流量
     *
     * @param lazyProxyFlow 代理流量领域对象
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在
     * @author Jia wei Wu
     * @date 2025/06/06 11:39 晚上
     **/

    @Override
    public Result<Boolean> exists(LazyProxyFlow lazyProxyFlow) {
        LazyProxyFlowDO lazyProxyFlowDO = LazyProxyFlowConverter.INSTANCE.fromLazyProxyFlow(lazyProxyFlow);
        Boolean exists = lazyLambdaStream.exists(LazyWrappers.lambdaWrapperBean(lazyProxyFlowDO));
        return ResultFactory.successOf(exists);
    }

    /**
     * 根据客户端查询流量
     *
     * @param size          分页大小
     * @param current       分页
     * @param lazyProxyFlow 查询条件
     * @return {@link Result<LazyPage<    LazyProxyFlow    >>} 分页访客端流量DTO对象
     */
    @Override
    public Result<LazyPage<LazyProxyFlow>> findPageGroupByClientId(int size, int current, LazyProxyFlow lazyProxyFlow) {
        LazyProxyFlowDO lazyProxyFlowDO = LazyProxyFlowConverter.INSTANCE.fromLazyProxyFlow(lazyProxyFlow);
        LazyPage<LazyProxyFlow> lazyPage = new LazyPage<>(current, size);
        LazyPage<LazyProxyFlow> visitorFlowLazyPage = lazyLambdaStream
                .selectPage(
                        LazyWrappers.lambdaWrapperBean(lazyProxyFlowDO)
                                .groupBy(LazyProxyFlowDO::getClientId),
                        lazyPage,
                        LazyProxyFlow.class);
        return ResultFactory.successOf(visitorFlowLazyPage);
    }

    /**
     * 根据客户端ID查询出 客户端所有的进出口流量
     *
     * @param clientIdList 客户端ID
     * @return 客户端所有的进出口流量
     */
    @Override
    public List<LazyProxyFlow> findListByClientIds(List<String> clientIdList) {
        if (ObjectUtils.isEmpty(clientIdList)) {
            return new ArrayList<>();
        }
        return lazyLambdaStream.selectList(
                LazyWrappers.<LazyProxyFlowDO>lambdaWrapper()
                        .in(LazyProxyFlowDO::getClientId, clientIdList),
                LazyProxyFlow.class);
    }

}